package com.yanqu.road.server.cmd.cookboy;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.pb.cookboy.CookBoyProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.cookboy.CookBoyModule;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyActivityMgr;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_COOK_BOY_SERVE_FOOD, desc = "上菜")
public class CookBoyServeFoodCmd extends AbstractCommand {

	int id;//成品食材的id，最终合成的7级菜
	int activityId;
	int no;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CookBoyProto.CookBoyServeFoodReqMsg reqMsg = CookBoyProto.CookBoyServeFoodReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		id = reqMsg.getId();
		no = reqMsg.getNo();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = CookBoyActivityMgr.getInTimeActivityInfo(activityId);
		if(activityInfo == null){
			ret = GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME;
			return;
		}
		if(!CookBoyActivityMgr.isActivityMatchTime(activityInfo)){
			ret = GameErrorCode.E_COOK_BOY_NOT_IN_MATCH_TIME;
			return;
		}
		ret = player.getModule(CookBoyModule.class).serveFood(activityId,no,id);
		CookBoyMgr.addSceneUser(activityId,player.getUserId(),0);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			CookBoyProto.CookBoyServeFoodRespMsg.Builder respMsg = CookBoyProto.CookBoyServeFoodRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_COOK_BOY_SERVE_FOOD, respMsg);
		}
	}
}
